package com.zzedu.manager.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.zzedu.common.core.text.Convert;
import com.zzedu.common.enums.CommonEnums;
import com.zzedu.common.enums.DeletedEnums;
import com.zzedu.common.utils.SecurityUtils;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.common.utils.security.LoginUser;
import com.zzedu.device.req.*;
import com.zzedu.device.rsp.*;
import com.zzedu.domain.*;
import com.zzedu.dto.subject.*;
import com.zzedu.enmuns.ConfigEnums;
import com.zzedu.enmuns.InteractionTemplateEnums;
import com.zzedu.enmuns.InteractionTypeEnums;
import com.zzedu.enmuns.SubjectTypeEnums;
import com.zzedu.manager.IZzPictureBookManager;
import com.zzedu.manager.IZzUploadFileManager;
import com.zzedu.result.Result;
import com.zzedu.result.ResultEnum;
import com.zzedu.service.*;
import com.zzedu.system.service.ISysConfigService;
import com.zzedu.utils.PageData;
import com.zzedu.vo.ZzUploadFileVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class ZzZzPictureBookManagerImpl implements IZzPictureBookManager {

    @Autowired
    IZzPictureBookService zzPictureBookService;

    @Autowired
    IZzUploadFileManager zzUploadFileManager;

    @Autowired
    IZzPictureNodeService zzPictureNodeService;

    @Autowired
    IZzSubjectContentService zzSubjectContentService;

    @Autowired
    ISysConfigService sysConfigService;

    @Autowired
    IZzTimetableService zzTimetableService;

    @Autowired
    IZzTeacherService zzTeacherService;

    @Autowired
    IZzClassesService zzClassesService;

    @Autowired
    IZzSchoolService zzSchoolService;

    @Autowired
    IZzOrganizationService zzOrganizationService;

    @Autowired
    IZzCourseService zzCourseService;


    @Override
    public Result<List<ApiZzPictureBookVO>> queryPage(QueryPageZzPictureBook queryPageZzPictureBook) {
        Result<List<ApiZzPictureBookVO>> result = new Result<>();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ZzTeacher zzTeacher = zzTeacherService.getById(loginUser.getUserId());
        Assert.isTrue(CommonEnums.YES == CommonEnums.getCode(zzTeacher.getState()), "老师状态异常");
        // 当前老师对应的学校#
        LambdaQueryWrapper<ZzTimetable> queryWrapper = Wrappers.lambdaQuery(ZzTimetable.class);
        queryWrapper.ge(null != queryPageZzPictureBook.getStartDate(), ZzTimetable::getStudyDate, queryPageZzPictureBook.getStartDate());
        queryWrapper.le(null != queryPageZzPictureBook.getEndDate(), ZzTimetable::getStudyDate, queryPageZzPictureBook.getEndDate());
        queryWrapper.eq(ZzTimetable::getSchoolId, zzTeacher.getSchoolId());
        if (null != queryPageZzPictureBook.getUnitId()) {
            queryWrapper.apply("book_id in (select id from zz_picture_book where show_model in(2,3) and deleted = {0} and level={1} and unit_id={2})", DeletedEnums.NO.getCode(), queryPageZzPictureBook.getLevel(), queryPageZzPictureBook.getUnitId());
        } else {
            queryWrapper.apply("book_id in (select id from zz_picture_book where show_model in(2,3) and deleted = {0} and level={1})", DeletedEnums.NO.getCode(), queryPageZzPictureBook.getLevel());

        }
        queryWrapper.apply("clzz_id in(select id from zz_classes where deleted=0)");
        queryWrapper.orderByAsc(ZzTimetable::getStudyDate).orderByAsc(ZzTimetable::getId);
        List<ZzTimetable> zzTimetables = zzTimetableService.list(queryWrapper);
        List<Long> bookIdList = zzTimetables.stream().map(ZzTimetable::getBookId).collect(Collectors.toCollection(LinkedHashSet::new)) // 利用 LinkedHashSet 去重并保持顺序
                .stream().collect(Collectors.toList());
        List<ApiZzPictureBookVO> list = Lists.newArrayList();


        for (Long bookId : bookIdList) {
            ZzPictureBook pictureBook = this.zzPictureBookService.getById(bookId);
            ApiZzPictureBookVO apiZzPictureBookVO = new ApiZzPictureBookVO();
            BeanUtils.copyProperties(pictureBook, apiZzPictureBookVO);
            List<Long> coverList = Optional.ofNullable(pictureBook.getCover()).map(json -> JSON.parseArray(json, Long.class)).orElse(Lists.newArrayList());
            List<Long> videoList = Optional.ofNullable(pictureBook.getVideo()).map(json -> JSON.parseArray(json, Long.class)).orElse(Lists.newArrayList());
            List<Long> allIdList = Lists.newArrayList();
            allIdList.addAll(coverList);
            allIdList.addAll(videoList);
            Map<Long, ZzUploadFileVO> fileMap = zzUploadFileManager.getMapByIdList(allIdList);
            if (CollectionUtils.isNotEmpty(coverList)) {
                apiZzPictureBookVO.setCoverZzUploadFileVO(Lists.newArrayList(fileMap.get(coverList.get(0))));
            }
            if (CollectionUtils.isNotEmpty(videoList)) {
                apiZzPictureBookVO.setVideoZzUploadFileVO(Lists.newArrayList(fileMap.get(videoList.get(0))));
            }
            ZzCourse zzCourse = zzCourseService.getById(pictureBook.getUnitId());
            if (null != zzCourse) {
                apiZzPictureBookVO.setUnitName(zzCourse.getName());
            }
            apiZzPictureBookVO.setApiClzzVOList(Lists.newArrayList());
            for (Iterator<ZzTimetable> iterator = zzTimetables.iterator(); iterator.hasNext(); ) {
                ZzTimetable zzTimetable = iterator.next();
                if (bookId.equals(zzTimetable.getBookId())) {
                    ZzClasses zzClasses = this.zzClassesService.getById(zzTimetable.getClzzId());
                    if (null == zzClasses) {
                        continue;
                    }
                    ApiClzzVO apiClzzVO = new ApiClzzVO();
                    apiClzzVO.setId(zzTimetable.getClzzId());
                    apiClzzVO.setName(zzClasses.getName());
                    apiClzzVO.setLevel(zzClasses.getLevel());
                    apiClzzVO.setEndDate(zzTimetable.getEndDate());
                    apiClzzVO.setStudyDate(zzTimetable.getStudyDate());
                    apiClzzVO.setStartDate(zzTimetable.getStartDate());
                    apiZzPictureBookVO.getApiClzzVOList().add(apiClzzVO);
                    iterator.remove();
                }
            }
            list.add(apiZzPictureBookVO);
        }
        result.setData(list);
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setResultMessage(ResultEnum.SUCCESS.getDesc());
        return result;
    }

    @Override
    public Result<List<ApiZzPictureBookVO>> preQueryPage(PreQueryPageZzPictureBook queryPageZzPictureBook) {
        Result<List<ApiZzPictureBookVO>> result = new Result<>();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ZzTeacher zzTeacher = zzTeacherService.getById(loginUser.getUserId());
        Assert.notNull(zzTeacher, "老师信息不存在");
        Assert.isTrue(CommonEnums.YES == CommonEnums.getCode(zzTeacher.getState()), "老师状态异常");
        ZzSchool zzSchool = zzSchoolService.getById(zzTeacher.getSchoolId());
        Assert.notNull(zzSchool, "学校信息不存在");
        Assert.isTrue(CommonEnums.YES == CommonEnums.getCode(zzSchool.getState()), "学校状态异常");
        if (null == zzSchool.getBookCount()) {
            zzSchool.setBookCount(2);
        }
        List<ApiZzPictureBookVO> list = Lists.newArrayList();
        if (StringUtils.isNotBlank(zzSchool.getLevel())) {
            List<String> levelList = JSON.parseArray(zzSchool.getLevel(), String.class);
            if (levelList.contains(queryPageZzPictureBook.getLevel())) {
                LambdaQueryWrapper<ZzCourse> zzCourseLambdaQueryWrapper = Wrappers.lambdaQuery(ZzCourse.class);
                zzCourseLambdaQueryWrapper.eq(ZzCourse::getState, CommonEnums.YES.getCode());
                zzCourseLambdaQueryWrapper.eq(ZzCourse::getLevel, queryPageZzPictureBook.getLevel());

                zzCourseLambdaQueryWrapper.eq(queryPageZzPictureBook.getUnitId() != null, ZzCourse::getId, queryPageZzPictureBook.getUnitId());
                zzCourseLambdaQueryWrapper.orderByAsc(ZzCourse::getSortNo);
                List<ZzCourse> zzCourseList = this.zzCourseService.list(Page.of(1, zzSchool.getBookCount()), zzCourseLambdaQueryWrapper);

                for (ZzCourse zzCourse : zzCourseList) {
                    LambdaQueryWrapper<ZzPictureBook> lambdaQueryWrapper = Wrappers.lambdaQuery(ZzPictureBook.class);
                    lambdaQueryWrapper.in(StringUtils.isBlank(zzSchool.getLevel()), ZzPictureBook::getLevel, Convert.toStrArray(zzSchool.getLevel()));
                    lambdaQueryWrapper.eq(ZzPictureBook::getUnitId, zzCourse.getId());
                    lambdaQueryWrapper.in(ZzPictureBook::getShowModel, Lists.newArrayList(2, 3));
                    lambdaQueryWrapper.orderByAsc(ZzPictureBook::getSortNo).orderByAsc(ZzPictureBook::getId);
                    List<ZzPictureBook> zzPictureBookList = this.zzPictureBookService.list(lambdaQueryWrapper);
                    for (ZzPictureBook pictureBook : zzPictureBookList) {
                        ApiZzPictureBookVO apiZzPictureBookVO = new ApiZzPictureBookVO();
                        BeanUtils.copyProperties(pictureBook, apiZzPictureBookVO);
                        List<Long> coverList = Optional.ofNullable(pictureBook.getCover()).map(json -> JSON.parseArray(json, Long.class)).orElse(Lists.newArrayList());
                        List<Long> videoList = Optional.ofNullable(pictureBook.getVideo()).map(json -> JSON.parseArray(json, Long.class)).orElse(Lists.newArrayList());
                        List<Long> allIdList = Lists.newArrayList();
                        allIdList.addAll(coverList);
                        allIdList.addAll(videoList);
                        Map<Long, ZzUploadFileVO> fileMap = zzUploadFileManager.getMapByIdList(allIdList);
                        if (CollectionUtils.isNotEmpty(coverList)) {
                            apiZzPictureBookVO.setCoverZzUploadFileVO(Lists.newArrayList(fileMap.get(coverList.get(0))));
                        }
                        if (CollectionUtils.isNotEmpty(videoList)) {
                            apiZzPictureBookVO.setVideoZzUploadFileVO(Lists.newArrayList(fileMap.get(videoList.get(0))));
                        }
                        apiZzPictureBookVO.setUnitName(zzCourse.getName());
                        apiZzPictureBookVO.setApiClzzVOList(Lists.newArrayList());
                        list.add(apiZzPictureBookVO);
                    }
                }
            }
        }
        result.setData(list);
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setResultMessage(ResultEnum.SUCCESS.getDesc());
        return result;
    }

    @Override
    public Result<List<ApiZzPictureBookNodeVO>> queryAllNode(QueryAllZzPictureNode data) {
        LambdaQueryWrapper<ZzPictureNode> lambdaQueryWrapper = Wrappers.lambdaQuery(ZzPictureNode.class);
        lambdaQueryWrapper.eq(ZzPictureNode::getPictureBookId, data.getId());
        lambdaQueryWrapper.eq(ZzPictureNode::getNodeType, data.getType());
        if (1 == data.getType()) {
            lambdaQueryWrapper.apply("id in(select node_id from zz_subject_content where deleted=0)");
        }
        lambdaQueryWrapper.orderByAsc(ZzPictureNode::getSortNo).orderByAsc(ZzPictureNode::getId);
        List<ZzPictureNode> zzPictureNodeList = zzPictureNodeService.list(lambdaQueryWrapper);
        Result<List<ApiZzPictureBookNodeVO>> result = new Result<>();
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setResultMessage(ResultEnum.SUCCESS.getDesc());
        String config = sysConfigService.selectConfigByKeyCache(ConfigEnums.TASK_PICTURE_NODE_ICON.getCode());
        JSONObject obj = new JSONObject();
        if (StringUtils.isNotBlank(config)) {
            obj = JSON.parseObject(config);
        }
        JSONObject finalObj = obj;
        result.setData(Optional.ofNullable(zzPictureNodeList).orElse(Lists.newArrayList()).stream().map(zzSubjectContent -> {
            ApiZzPictureBookNodeVO apiZzPictureBookNodeVO = new ApiZzPictureBookNodeVO();
            apiZzPictureBookNodeVO.setId(zzSubjectContent.getId());
            apiZzPictureBookNodeVO.setNodeName(zzSubjectContent.getNodeName());
            apiZzPictureBookNodeVO.setIconNURL(MapUtils.getString(finalObj, apiZzPictureBookNodeVO.getNodeName() + "_n", ""));
            apiZzPictureBookNodeVO.setIconSURL(MapUtils.getString(finalObj, apiZzPictureBookNodeVO.getNodeName() + "_s", ""));
            return apiZzPictureBookNodeVO;
        }).collect(Collectors.toList()));
        return result;
    }

    @Override
    public Result<List<ApiZzPictureContentVO>> queryPictureByNodeContent(QueryPictureByNodeContent data) {
        LambdaQueryWrapper<ZzSubjectContent> lambdaQueryWrapper = Wrappers.lambdaQuery(ZzSubjectContent.class);
        lambdaQueryWrapper.select(ZzSubjectContent::getId);
        lambdaQueryWrapper.eq(ZzSubjectContent::getState, CommonEnums.YES.getCode());
        lambdaQueryWrapper.apply("node_id in(select id from zz_picture_node where  deleted={0} and id={1})", DeletedEnums.NO.getCode(), data.getNodeId());
        lambdaQueryWrapper.orderByAsc(ZzSubjectContent::getSortNo);
        List<ZzSubjectContent> zzSubjectContentList = zzSubjectContentService.list(lambdaQueryWrapper);
        Result<List<ApiZzPictureContentVO>> result = new Result<>();
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setResultMessage(ResultEnum.SUCCESS.getDesc());
        result.setData(Optional.ofNullable(zzSubjectContentList).orElse(Lists.newArrayList()).stream().map(zzSubjectContent -> {
            ApiZzPictureContentVO apiZzPictureContentVO = new ApiZzPictureContentVO();
            apiZzPictureContentVO.setId(zzSubjectContent.getId());
            return apiZzPictureContentVO;
        }).collect(Collectors.toList()));
        return result;
    }

    @Override
    public Result<PageData<ApiZzPictureBookVO>> teacherPictureBookPageList(TeacherPictureBookPageList data) {
        Result<PageData<ApiZzPictureBookVO>> result = new Result<>();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ZzTeacher zzTeacher = zzTeacherService.getById(loginUser.getUserId());
        Assert.isTrue(CommonEnums.YES == CommonEnums.getCode(zzTeacher.getState()), "老师状态异常");
        ZzSchool zzSchool = zzSchoolService.getById(zzTeacher.getSchoolId());
        Assert.notNull(zzSchool, "学校信息不存在");
        Assert.isTrue(CommonEnums.YES == CommonEnums.getCode(zzSchool.getState()), "学校状态异常");
        IPage<ZzPictureBook> page = zzPictureBookService.apiClassQueryZzPictureBookPage(Page.of(data.getPageNum(), data.getPageSize()), zzSchool.getId(), data.getId(), LocalDateTime.now());
        PageData<ApiZzPictureBookVO> pageData = new PageData<>();
        pageData.setTotal(pageData.getTotal());
        pageData.setRowData(Lists.newArrayList());
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            for (ZzPictureBook pictureBook : page.getRecords()) {
                ApiZzPictureBookVO apiZzPictureBookVO = new ApiZzPictureBookVO();
                BeanUtils.copyProperties(pictureBook, apiZzPictureBookVO);
                List<Long> coverList = Optional.ofNullable(pictureBook.getCover()).map(json -> JSON.parseArray(json, Long.class)).orElse(Lists.newArrayList());
                List<Long> videoList = Optional.ofNullable(pictureBook.getVideo()).map(json -> JSON.parseArray(json, Long.class)).orElse(Lists.newArrayList());
                List<Long> allIdList = Lists.newArrayList();
                allIdList.addAll(coverList);
                allIdList.addAll(videoList);
                Map<Long, ZzUploadFileVO> fileMap = zzUploadFileManager.getMapByIdList(allIdList);
                if (CollectionUtils.isNotEmpty(coverList)) {
                    apiZzPictureBookVO.setCoverZzUploadFileVO(Lists.newArrayList(fileMap.get(coverList.get(0))));
                }
                if (CollectionUtils.isNotEmpty(videoList)) {
                    apiZzPictureBookVO.setVideoZzUploadFileVO(Lists.newArrayList(fileMap.get(videoList.get(0))));
                }

                pageData.getRowData().add(apiZzPictureBookVO);
            }
        }
        result.setData(pageData);
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setResultMessage(ResultEnum.SUCCESS.getDesc());
        return result;
    }

    @Override
    public Result<List<DownDataVO>> queryDownData(QueryDownDataReq data) {
        LambdaQueryWrapper<ZzSubjectContent> lambdaQueryWrapper = Wrappers.lambdaQuery(ZzSubjectContent.class);
        lambdaQueryWrapper.eq(ZzSubjectContent::getState, CommonEnums.YES.getCode());
        lambdaQueryWrapper.apply("node_id in(select id from zz_picture_node where node_type=1 and deleted={0} and picture_book_id={1})", DeletedEnums.NO.getCode(), data.getId());
        List<ZzSubjectContent> zzSubjectContentList = zzSubjectContentService.list(lambdaQueryWrapper);
        List<DownDataVO> downDataVOList = Lists.newArrayList();
        for (ZzSubjectContent zzSubjectContent : zzSubjectContentList) {
            List<DownDataVO> downDataVOS = getResourceList(zzSubjectContent);
            if (CollectionUtils.isNotEmpty(downDataVOS)) {
                downDataVOList.addAll(downDataVOS);
            }
        }
        Result<List<DownDataVO>> result = new Result<>();
        result.setData(downDataVOList);
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setResultMessage(ResultEnum.SUCCESS.getDesc());
        return result;
    }

    @Override
    public Result<PageData<ApiZzPictureBookVO>> queryPageNews(QueryPageZzPictureBook queryPageZzPictureBook) {
        Result<PageData<ApiZzPictureBookVO>> result = new Result<>();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ZzTeacher zzTeacher = zzTeacherService.getById(loginUser.getUserId());
        Assert.isTrue(CommonEnums.YES == CommonEnums.getCode(zzTeacher.getState()), "老师状态异常");

        List<Integer> stateList = Lists.newArrayList();
        stateList.add(CommonEnums.YES.getCode());
        stateList.add(CommonEnums.NO.getCode());
        IPage<ZzPictureBook> page = null;
        if (this.zzSchoolService.isTestSchoolFlag(zzTeacher.getSchoolId())) {
            page = this.zzPictureBookService.page(Page.of(queryPageZzPictureBook.getPageNum(), queryPageZzPictureBook.getPageSize()), Wrappers.lambdaQuery(ZzPictureBook.class).eq(StringUtils.isNotBlank(queryPageZzPictureBook.getLevel()), ZzPictureBook::getLevel, queryPageZzPictureBook.getLevel()).eq(null != queryPageZzPictureBook.getUnitId(), ZzPictureBook::getUnitId, queryPageZzPictureBook.getUnitId()).orderByAsc(ZzPictureBook::getSortNo));
        } else {
            page = zzPictureBookService.apiDeviceZzPictureBookQueryPageNews(Page.of(queryPageZzPictureBook.getPageNum(), queryPageZzPictureBook.getPageSize()), queryPageZzPictureBook.getLevel(), zzTeacher.getSchoolId(), queryPageZzPictureBook.getUnitId(), stateList);
        }
        List<ApiZzPictureBookVO> list = Lists.newArrayList();
        for (ZzPictureBook pictureBook : page.getRecords()) {
            ApiZzPictureBookVO apiZzPictureBookVO = new ApiZzPictureBookVO();
            BeanUtils.copyProperties(pictureBook, apiZzPictureBookVO);
            List<Long> coverList = Optional.ofNullable(pictureBook.getCover()).map(json -> JSON.parseArray(json, Long.class)).orElse(Lists.newArrayList());
            List<Long> videoList = Optional.ofNullable(pictureBook.getVideo()).map(json -> JSON.parseArray(json, Long.class)).orElse(Lists.newArrayList());
            List<Long> allIdList = Lists.newArrayList();
            allIdList.addAll(coverList);
            allIdList.addAll(videoList);
            Map<Long, ZzUploadFileVO> fileMap = zzUploadFileManager.getMapByIdList(allIdList);
            if (CollectionUtils.isNotEmpty(coverList)) {
                apiZzPictureBookVO.setCoverZzUploadFileVO(Lists.newArrayList(fileMap.get(coverList.get(0))));
            }
            if (CollectionUtils.isNotEmpty(videoList)) {
                apiZzPictureBookVO.setVideoZzUploadFileVO(Lists.newArrayList(fileMap.get(videoList.get(0))));
            }
            ZzCourse zzCourse = zzCourseService.getById(pictureBook.getUnitId());
            if (null != zzCourse) {
                apiZzPictureBookVO.setUnitName(zzCourse.getName());
            }
            apiZzPictureBookVO.setApiClzzVOList(Lists.newArrayList());
            list.add(apiZzPictureBookVO);
        }
        PageData<ApiZzPictureBookVO> pageData = new PageData<>();
        pageData.setTotal(pageData.getTotal());
        pageData.setRowData(list);
        result.setData(pageData);
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setResultMessage(ResultEnum.SUCCESS.getDesc());
        return result;

    }

    @Override
    public Result<PageData<ApiZzPictureBookVO>> queryPageToExMini(QueryPageZzPictureBook queryPageZzPictureBook) {
        Result<PageData<ApiZzPictureBookVO>> result = new Result<>();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ZzTeacher zzTeacher = zzTeacherService.getById(loginUser.getUserId());
        Assert.isTrue(CommonEnums.YES == CommonEnums.getCode(zzTeacher.getState()), "老师状态异常");
        List<Integer> stateList = Lists.newArrayList();
        stateList.add(CommonEnums.YES.getCode());
        stateList.add(CommonEnums.NO.getCode());
        IPage<ZzPictureBook> page = zzPictureBookService.apiMiniProZzPictureBookQueryPageNews(Page.of(queryPageZzPictureBook.getPageNum(), queryPageZzPictureBook.getPageSize()), queryPageZzPictureBook.getLevel(), zzTeacher.getSchoolId(), queryPageZzPictureBook.getUnitId(), stateList);
        List<ApiZzPictureBookVO> list = Lists.newArrayList();
        for (ZzPictureBook pictureBook : page.getRecords()) {
            ApiZzPictureBookVO apiZzPictureBookVO = new ApiZzPictureBookVO();
            BeanUtils.copyProperties(pictureBook, apiZzPictureBookVO);
            List<Long> coverList = Optional.ofNullable(pictureBook.getCover()).map(json -> JSON.parseArray(json, Long.class)).orElse(Lists.newArrayList());
            List<Long> videoList = Optional.ofNullable(pictureBook.getVideo()).map(json -> JSON.parseArray(json, Long.class)).orElse(Lists.newArrayList());
            List<Long> allIdList = Lists.newArrayList();
            allIdList.addAll(coverList);
            allIdList.addAll(videoList);
            Map<Long, ZzUploadFileVO> fileMap = zzUploadFileManager.getMapByIdList(allIdList);
            if (CollectionUtils.isNotEmpty(coverList)) {
                apiZzPictureBookVO.setCoverZzUploadFileVO(Lists.newArrayList(fileMap.get(coverList.get(0))));
            }
            if (CollectionUtils.isNotEmpty(videoList)) {
                apiZzPictureBookVO.setVideoZzUploadFileVO(Lists.newArrayList(fileMap.get(videoList.get(0))));
            }
            ZzCourse zzCourse = zzCourseService.getById(pictureBook.getUnitId());
            if (null != zzCourse) {
                apiZzPictureBookVO.setUnitName(zzCourse.getName());
            }
            apiZzPictureBookVO.setApiClzzVOList(Lists.newArrayList());
            list.add(apiZzPictureBookVO);
        }
        PageData<ApiZzPictureBookVO> pageData = new PageData<>();
        pageData.setTotal(pageData.getTotal());
        pageData.setRowData(list);
        result.setData(pageData);
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setResultMessage(ResultEnum.SUCCESS.getDesc());
        return result;
    }

    private List<DownDataVO> getResourceList(ZzSubjectContent zzSubjectContent) {
        List<DownDataVO> downDataVOList = Lists.newArrayList();
        SubjectTypeEnums subjectTypeEnums = SubjectTypeEnums.getCode(zzSubjectContent.getSubjectType());
        String content = zzSubjectContent.getContent();
        List<Long> fileIdList = Lists.newArrayList();
        if (SubjectTypeEnums.video == subjectTypeEnums) {
            ContentVideoDTO contentVideoDTO = JSON.parseObject(content, ContentVideoDTO.class);
            if (contentVideoDTO.getFileId() != null) {
                // 异步根据 fileId 获取文件信息
                fileIdList.add(contentVideoDTO.getFileId());
            }
            if (null != contentVideoDTO.getImageFileId()) {
                fileIdList.add(contentVideoDTO.getImageFileId());
            }
            if (null != contentVideoDTO.getVideoFlag() && 0 == contentVideoDTO.getVideoFlag()) {
                ZzPictureNode zzPictureNode = zzPictureNodeService.getById(zzSubjectContent.getNodeId());
                if (null != zzPictureNode) {
                    ZzPictureBook zzPictureBook = this.zzPictureBookService.getById(zzPictureNode.getPictureBookId());
                    if (null != zzPictureBook) {
                        List<Long> fileIdLists = JSON.parseArray(zzPictureBook.getVideo(), Long.class);
                        if (CollectionUtils.isNotEmpty(fileIdLists)) {
                            fileIdList.addAll(fileIdLists);
                        }
                    }
                }
            }
        } else if (SubjectTypeEnums.image == subjectTypeEnums) {
            ContentImageDTO contentImageDTO = JSON.parseObject(content, ContentImageDTO.class);
            List<Long> imagFileIdList = Stream.concat(Optional.ofNullable(contentImageDTO.getMusicFileId()).orElse(Lists.newArrayList()).stream(), Stream.of(contentImageDTO.getImageFileId(), contentImageDTO.getAudioFileId()).filter(Objects::nonNull)).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(imagFileIdList)) {
                fileIdList.addAll(imagFileIdList);
            }
        } else if (SubjectTypeEnums.interaction == subjectTypeEnums) {
            InteractionTypeEnums interactionTypeEnums = InteractionTypeEnums.getCode(zzSubjectContent.getInteraction());
            InteractionTemplateEnums interactionTemplateEnums = InteractionTemplateEnums.getCode(zzSubjectContent.getTemplate());
            if (InteractionTypeEnums.line == interactionTypeEnums) {
                ContentInteractionLineDTO interactionLineDTO = JSON.parseObject(content, ContentInteractionLineDTO.class);
                if (InteractionTemplateEnums.line_custom == interactionTemplateEnums) {
                    List<Long> mediaFileIdList = Lists.newArrayList(interactionLineDTO.getImageFileId(), interactionLineDTO.getAudioFileId()

                    ).stream().filter(Objects::nonNull).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(mediaFileIdList)) {
                        fileIdList.addAll(mediaFileIdList);
                    }
                    fileIdList.addAll(interactionLineDTO.getFirstMainImageFileIdList());
                    fileIdList.addAll(interactionLineDTO.getSecondMainImageFileIdList());
                }
            } else if (InteractionTypeEnums.drag == interactionTypeEnums) {
                ContentDragContentDTO contentDragDTO = JSON.parseObject(content, ContentDragContentDTO.class);
                //处理图片
                List<Long> mediaFileIdList = Stream.of(contentDragDTO.getImageFileId(), contentDragDTO.getAudioFileId()).filter(Objects::nonNull).collect(Collectors.toList());
                mediaFileIdList.addAll(contentDragDTO.getDragFileIdList());
                mediaFileIdList = mediaFileIdList.stream().filter(Objects::nonNull).toList();
                if (CollectionUtils.isNotEmpty(mediaFileIdList)) {
                    fileIdList.addAll(mediaFileIdList);
                }
            } else if (InteractionTypeEnums.mask_layer == interactionTypeEnums) {
                ContentInteractionMaskLayerDTO contentInteractionMaskLayerDTO = JSON.parseObject(content, ContentInteractionMaskLayerDTO.class);
                // 定义跳过 key 列表
                List<String> skipKeys = Arrays.asList("mask_layer_top", "mask_layer_bottom", "show_model");
                Map<String, String> maskLayer = Optional.ofNullable(contentInteractionMaskLayerDTO.getMaskLayer()).orElse(Collections.emptyMap());
                // 收集所有需要加载文件的 fileId
                List<Long> mediaFileIdList = maskLayer.entrySet().stream().filter(e -> !skipKeys.contains(e.getKey())).map(Map.Entry::getValue).map(val -> {
                    try {
                        return Long.valueOf(val);
                    } catch (NumberFormatException ex) {
                        return null;
                    }
                }).filter(Objects::nonNull).collect(Collectors.toList());
                Optional.ofNullable(contentInteractionMaskLayerDTO.getImageFileId()).ifPresent(mediaFileIdList::add);
                //处理图片
                if (CollectionUtils.isNotEmpty(mediaFileIdList)) {
                    fileIdList.addAll(mediaFileIdList);
                }
            }
        }
        List<ZzUploadFileVO> zzUploadFileVOList = zzUploadFileManager.getByIdList(fileIdList);
        for (ZzUploadFileVO zzUploadFileVO : zzUploadFileVOList) {
            DownDataVO downDataVO = new DownDataVO();
            downDataVO.setId(zzUploadFileVO.getId());
            downDataVO.setFileExt(zzUploadFileVO.getFileExt());
            downDataVO.setUrl(zzUploadFileVO.getUrl());
            downDataVO.setFileName(zzUploadFileVO.getFileName());
            downDataVOList.add(downDataVO);
        }
        return downDataVOList;
    }
}
